<?php
class ControllerModuleCategoryProductList extends Controller {
	public function index($setting) {
		$this->load->language('module/featured');

		$data['heading_title'] = $this->language->get('heading_title');

		$data['text_tax'] = $this->language->get('text_tax');

		$data['button_cart'] = $this->language->get('button_cart');
		$data['button_wishlist'] = $this->language->get('button_wishlist');
		$data['button_compare'] = $this->language->get('button_compare');

		if (isset($setting['title'][$this->config->get('config_language_id')])) {
			$data['heading_title'] = $setting['title'][$this->config->get('config_language_id')];
		} else {
			$data['heading_title'] = false;
		}

		$this->load->model('catalog/category');
		$this->load->model('catalog/product');

		$this->load->model('tool/image');

		$data['categories'] = array();

		if (!$setting['limit'] || !(int)$setting['limit']) {
			$setting['limit'] = 4;
		}

		if (!empty($setting['category'])) {
			foreach ($setting['category'] as $category_id) {
				$category_info = $this->model_catalog_category->getCategory($category_id);

				if (!$category_info) {
					continue;
				}

				$filter_data = array(
					'filter_category_id' => $category_id,
					'sort' => 'p.date_added',
					'start' => 0,
					'limit' => $setting['limit'],
				);

				$products_data = $this->model_catalog_product->getProducts($filter_data);

				if (!$products_data) {
					continue;
				}

				$products = array();

				foreach ($products_data as $product_info) {
					if ($product_info['image'] && is_file(DIR_IMAGE . $product_info['image'])) {
						$image = $this->model_tool_image->resize($product_info['image'], $setting['width'], $setting['height']);
					} else {
						$image = $this->model_tool_image->resize('placeholder.png', $setting['width'], $setting['height']);
					}

					if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
						$price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
					} else {
						$price = false;
					}

					if ((float)$product_info['special']) {
						$special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
					} else {
						$special = false;
					}

					if ($this->config->get('config_tax')) {
						$tax = $this->currency->format((float)$product_info['special'] ? $product_info['special'] : $product_info['price']);
					} else {
						$tax = false;
					}

					if ($this->config->get('config_review_status')) {
						$rating = $product_info['rating'];
					} else {
						$rating = false;
					}

					$products[] = array(
						'product_id'  => $product_info['product_id'],
						'thumb'       => $image,
						'name'        => $product_info['name'],
						'price'       => $price,
						'special'     => $special,
						'tax'         => $tax,
						'rating'      => $rating,
						'href'        => $this->url->link('product/product', 'product_id=' . $product_info['product_id'])
					);
				}

				if ($products) {
					$data['categories'][] = array(
						'name' => $category_info['name'],
						'href' => $this->url->link('product/category', 'category_id=' . $category_info['category_id']),
						'products' => $products,
					);
				}
			}
		}

		if ($data['categories']) {
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/category_product_list.tpl')) {
				return $this->load->view($this->config->get('config_template') . '/template/module/category_product_list.tpl', $data);
			} else {
				return $this->load->view('default/template/module/category_product_list.tpl', $data);
			}
		}
	}
}